package cn.gqr.controller.adm;

import cn.gqr.entity.BookType;
import cn.gqr.service.TypeService;
import cn.gqr.tool.RespJson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

//后台类型api
@Controller
@RequestMapping("/api/adm/type")
public class TypeController {

    @Autowired
    TypeService typeService;

    //添加修改
    @ResponseBody
    @PostMapping("/add")
    public Map<String, Object> add(BookType book) {
        return typeService.addOrUpdate(book);
    }

    //全查询
    @ResponseBody
    @RequestMapping("/findAll")
    public Map<String, Object> findAll() {
        List<BookType> bookTypes = typeService.findAll();
        return RespJson.pack(200, "ok", bookTypes);
    }

    //全查询
    @ResponseBody
    @RequestMapping("/findAll2")
    public Map<String, Object> findAll2() {
        List<BookType> bookTypes = typeService.findAll2();
        return RespJson.pack(200, "ok", bookTypes);
    }

    //id查询
    @ResponseBody
    @RequestMapping("/findById/{id}")
    public Map<String, Object> findById(@PathVariable("id") Integer id) {
        BookType bookType = typeService.findById(id);
        if (bookType == null) {
            return RespJson.pack(204, "no", null);
        }
        return RespJson.pack(200, "ok", bookType);
    }

}
